<?php

namespace app\index\controller;

use app\index\model\Users;
use app\index\model\VerifyCode;
use app\index\model\SmsLog;
use think\Controller;
use app\index\model\Hxcall;

class Index extends Controller
{

    /**
     * 用户登陆页面
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 用户注册页面
     * @return mixed
     */
    public function register()
    {

        return $this->fetch();
    }

    /**
     *
     * @return mixed
     */
    public function selectOpera()
    {
        if (empty($this->session('user_id'))) {
            $this->error('请先登录');
        }

        return $this->fetch('selectopera');
    }

    //发布帖子公共
    private function post_common($channel_type)
    {
        $user_id = intval(session('user_id'));
        $channel_id = 32;

        if (!$channel_id) return json(array('code' => 1, 'msg' => '帖子发布失败，请重试'));

        $channel = model('Channel')->getChannelInfo('channel_id =' . $channel_id);
        if ($channel['channel_type'] != $channel_type) return json(array('code' => 1, 'msg' => '帖子发布失败，请重试'));
        //未关注不能发
        $is_follow = model('Follow')->checkFollowChannel($user_id, $channel_id);

//        var_dump($is_follow);
        if (!$is_follow) model('Follow')->save(array('user_id' => $user_id,'followed_id'  => $channel_id,'addtime' => time(),'follow_type' => 1));

        $title = trim(input('post.title', ''));
        $content = trim(input('post.content', ''));
        $imgs = input('post.imgs', '');
        $imgs = trim($imgs, ',');
//        if (!$title) {
//            return json(array('code' => 1, 'msg' => '标题必须填写'));
//        }

        $clickdot = rand(3, 5);


        $arr = array(
            'title' => $title,
            'content' => $content,
            'imgs' => $imgs,
            'user_id' => $user_id,
            'channel_id' => $channel_id,
            'post_type' => $channel_type,
            'clickdot' => $clickdot,
        );

        return $arr;
    }

    /**
     * sama发帖页面
     * @return mixed
     */
    public function posts()
    {
        $user_id = intval(session('user_id'));
        if (empty($user_id)) {
            $this->error('请先登录');
        }

        if ($this->instance()->isAjax() && $this->instance()->isPost()) {
            // dump($_POST);die;
            $common_arr = $this->post_common(2);

            $hide_content = trim(input('post.hide_content'));
            $hide_imgs = input('hide_imgs', '');
            $hide_imgs = trim($hide_imgs, ',');
            $hide_price = round(input('price', 0.0, ''),2);
            if (!$hide_content && !$hide_imgs) {
                return json(array('code' => 1, 'msg' => '隐藏内容处内容、图片必须填写一项'));
            }

//            var_dump($hide_price <= 0.0);
            if ($hide_price <= 0.0) {
                return json(array('code' => 1, 'msg' => '请填写正确的价格'));
            }
            //  出错

            if ($p_id = model('Post')->addPost($common_arr)) {
                //添加隐藏内容

                $hide_arr = array(
                    'content' => $hide_content,
                    'user_id' => $user_id,
                    'price' => $hide_price,
                    'hide_type' => 1,
                    'id' => $p_id,
                    'imgs' => $hide_imgs,
                );

                model('PraiseHide')->addPraiseHide($hide_arr);


                return json(array('code' => 0, 'msg' => '帖子发布成功'));
            }
            return json(array('code' => 1, 'msg' => '帖子发布失败，请重试'));
            //

        }
        $channel_id = input('channel_id', 0, 'intval');

        $this->assign('channel_id', $channel_id);
        $this->assign('opt', 'praise');
        $this->assign('head_title', '发帖/楼主回帖');

        return $this->fetch('post');
    }

    /**
     * 钱包页面
     * @return mixed
     */
    public function wallet()
    {

        $user_id = $this->session('user_id');

        if (empty($user_id)) {
            $this->error('请先登录');
        }

        $admire_money = model('Users')->gitUserInfos('user_id', $user_id, 'left_money')['left_money'];
//	var_dump($user_id);
        $this->assign('admire_money', $admire_money);

        return $this->fetch();
    }


    /**
     * sama 隐私政策
     * @return mixed
     */
    public function samaPrivacy()
    {

        return $this->fetch();
    }

    /**
     * 用户注册协议
     * @return mixed
     */
    public function samaregister()
    {

        return $this->fetch();
    }

    /**
     * 判断登陆
     */
    public function login()
    {

        if ($this->instance()->isAjax() && $this->instance()->isPost()) {

            $mobile = trim(input('post.mobile'));
            $password = strtoupper(md5(trim(input('post.password'))));

            $user_obj = new Users();

            $user_info = $user_obj->gitUserId($mobile);

//	    var_dump(session('user_id'));
            if (!empty($user_info)) {

                if ($user_info['password'] == $password) {
                    $data['code'] = 1;
                    $data['msg'] = "登陆成功";
                    return json($data);
                }
                $data['code'] = 2;
                $data['msg'] = "密码错误";
                return json($data);
            } else {
                $data['code'] = 3;
                $data['msg'] = "用户不存在";
                return json($data);
            }
        }

    }


    /**
     * 用户注册判断
     * @return
     */
    public function getUserRegisterInfo()
    {

        if ($this->instance()->isAjax() && $this->instance()->isPost()) {

            $user_obj = new Users();
            $nickname = trim(input('post.nickname'));
            if (!$nickname) return json(array('code' => 1, 'msg' => '昵称不能为空'));
            $sex = input('post.sex');
            if (!$sex) return json(array('code' => 1, 'msg' => '性别不能为空'));
            $mobile = trim(input('post.mobile'));
            $password =  strtoupper(md5(trim(input('post.password'))));
            $verify_code = trim(input('post.verify_code'));
            $headimgurl = input('post.headimgurl');


            //手机号注册判断
            if (!empty(model('Users')->gitUserInfos('nickname', $nickname)))
                return json(array('code' => 1, 'msg' => "已存在昵称"));


            if (model('Users')->gitUserMobile($mobile))
                return json(array('code' => 1, 'msg' => '该手机号已存在'));

            $verify_code_obj = new VerifyCode();
            if ($verify_code_obj->getVerifyCodeInfos('mobile = ' . $mobile, 'verify_code')['verify_code'] != $verify_code) {

                return json(array('code' => 1, 'msg' => '验证码不正确'));
            }

            //注册
            $arr = array(
                'nickname' => $nickname,
                'sex' => $sex,
                'headimgurl' => $headimgurl,
                'password' => $password,
                'mobile' => $mobile,

            );
            $arr['mobile_registered'] = 1;

            //注册环信
            $rs = new Hxcall();
            $r = $rs->hx_register($mobile, $mobile, $nickname);

            if ($r) {
                $arr['hx_account'] = $mobile;
                $arr['hx_password'] = $mobile;
                $arr['hx_nickname'] = $nickname;
            }

            if ($user_obj->addUser($arr)) {
                $datas['code'] = 0;
                $datas['msg'] = '注册成功';
            } else {
                return json(array('code' => 1, 'msg' => '注册失败'));
            }


            $user_info = $user_obj->gitUserInfo('user_id', 'mobile = ' . $mobile);
            $this->session('user_id', $user_info['user_id']);

            return json($datas);
        }

    }

    /**
     * 发送验证码短信
     * @param array $params
     * @return 成功返回'发送成功'，失败返回错误码
     * @todo 发送验证码短信
     */
    public function sendVerifyCode()
    {
        //获取验证码
        if ($this->instance()->isAjax() && $this->instance()->isPost()) {
            $params = trim(input('post.mobile'));
            $user_obj = new Users();
            if ($user_obj->gitUserMobile($params)) {
                return json(array('code' => 1, 'msg' => '该手机号已存在'));
            }

            $verify_code_obj = new VerifyCode();

            $verify_code = $verify_code_obj->generateVerifyCode($params);
            if ($verify_code) {

                $sms_obj = new SmsLog();
                $send_state = $sms_obj->sendVerifyCode($params, $verify_code);

                if ($send_state['status'] == 0) {
                    return json(array('code' => 0, 'msg' => '验证码发送成功'));
                } else {
                    return json(array('code' => 1, 'msg' => '验证码发送失败'));
                }
            } else {
                return json(array('code' => 1, 'msg' => '请不要重复点击'));
            }
        }
    }






    /**
     * 用户充值页面
     * @return mixed
     */
    public function wechatpay()
    {
        return $this->fetch();
    }
}
